// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.query.ui;

import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DatabaseDiagram;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-15 qiang.zhang $
 * 
 */
public class ColumnSelectDialog extends Dialog {

    DatabaseDiagram dbDiagram;

    private Text nameLabel;

    private String columnname = "";

    /**
     * Qiang.Zhang.Adolf@gmail.com ColumnSelectDialog constructor comment.
     * 
     * @param parentShell
     * @param dbDiagram
     */
    public ColumnSelectDialog(Shell parentShell, DatabaseDiagram dbDiagram) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE | SWT.DIALOG_TRIM);
        this.dbDiagram = dbDiagram;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite createDialogArea = (Composite) super.createDialogArea(parent);
        Composite pattern = new Composite(createDialogArea, SWT.NONE);
        GridLayout gridLayout = new GridLayout(3, false);
        pattern.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        pattern.setLayoutData(gridData);
        final Button radioTable = new Button(pattern, SWT.RADIO);
        radioTable.setText("Table Column");

        final Button radioComputed = new Button(pattern, SWT.RADIO);
        radioComputed.setText("Computed Column");
        final Button openComputed = new Button(pattern, SWT.FLAT);
        openComputed.setText("...");
        openComputed.setEnabled(false);

        final Composite nameComp = new Composite(createDialogArea, SWT.NONE);
        gridLayout = new GridLayout(2, false);
        nameComp.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        nameComp.setLayoutData(gridData);
        Label label = new Label(nameComp, SWT.NONE);
        label.setText("Name:");
        nameLabel = new Text(nameComp, SWT.BORDER);
        nameLabel.setEditable(false);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        nameLabel.setLayoutData(gridData);
        Composite columns = new Composite(createDialogArea, SWT.NONE);
        gridLayout = new GridLayout();
        columns.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_BOTH);
        columns.setLayoutData(gridData);
        label = new Label(nameComp, SWT.NONE);
        label.setText("Columns:");
        final TreeViewer treeViewe = QueryUiUtils.getInstance().createDbDiagramTree(columns, dbDiagram);
        treeViewe.addSelectionChangedListener(new ISelectionChangedListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
             */
            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                Object firstElement = selection.getFirstElement();
                if (firstElement instanceof DBColumn) {
                    DBColumn column = (DBColumn) firstElement;
                    nameLabel.setText(column.getTable().getName() + "." + column.getName());
                }
            }
        });

        radioTable.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (radioTable.getSelection()) {
                    openComputed.setEnabled(false);
                    treeViewe.getTree().setEnabled(true);
                }
            }
        });
        radioComputed.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (radioComputed.getSelection()) {
                    openComputed.setEnabled(true);
                    treeViewe.getTree().setEnabled(false);
                }
            }
        });

        openComputed.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                ComputedColumnDialog dialog = new ComputedColumnDialog(getShell(), dbDiagram);
                if (ComputedColumnDialog.OK == dialog.open()) {
                    nameLabel.setText(dialog.getExpression());
                }
            }
        });
        nameLabel.addModifyListener(new ModifyListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
             */
            public void modifyText(ModifyEvent e) {
                columnname = nameLabel.getText();
            }

        });
        return createDialogArea;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Select Column");
        newShell.setSize(400, 600);
    }

    public String getColumnname() {
        return this.columnname;
    }

}
